<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/4/18
 * Time: 20:11
 */
namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Page;

/**
 * 营销策划控制器
 * Class Plan
 * @package app\index\controller
 */
class Plan extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $id=!empty($this->request->param('id'))?intval($this->request->param('id')):0;
        if(empty($id))$this->error('文章分类不存在！',url('/index'));

        $cpage=!empty($this->request->param('cpage'))?intval($this->request->param('cpage')):1;
        $total=!empty($this->request->param('total'))?intval($this->request->param('total')):12;
        $identify=!empty($this->request->param('identify'))?trim($this->request->param('identify')):'index';

        //文章分类信息作为页面信息
        $pageInfo=db('article_category')->where('id='.$id)->field('id,title,keywords,description,name,info,pid')->find();
        if(empty($pageInfo)) $this->error('文章分类不存在！',url('/index'));

        //根据当前控制器标识查询顶级导航信息
        $parentNavigation=db('navigation')->where('controllername="'.$this->identify.'" and pid=0')->field('name')->find();

        $start=($cpage-1)*$total;
        $url='/plan/'.$identify.'/'.$id.'/';

        //分类下所有文章
        $articleList=db('article')->where('category_id='.$id)->order('sort desc,id desc')->field('id,title,thumb,info,from_unixtime(createtime,"%Y-%m-%d") as createtime,avatar')->limit($start,$total)->select();
        $count=db('article')->where('category_id='.$id)->count();

        //分页
        $page=new Page();
        $show=$page->create($count,$cpage,$total,$url);

        //banner
        $bannerList=db('plan_banner')->order('sort desc,id asc')->field('title,thumb,link')->select();

        //父分类下的子分类
        $childrenPlist=db('article_category')->where('pid='.$pageInfo['pid'])->order('sort desc,id asc')->field('id,name')->select();

        //热门阅读
        $hotArticleList=db('article')->order('clicks desc,sort desc,id desc')->field('id,title,thumb,createtime')->limit(4)->select();
        if(!empty($hotArticleList)){
            foreach ($hotArticleList as &$hot_v){
                $hot_v['articleCommentCount']=db('article_comment')->where('article_id='.$hot_v['id'])->count();
            }
            unset($v);
        }

        //所有分类
        $categoryList=db('article_category')->order('sort desc,id desc')->field('id,name')->select();

        //运行天数
        $admin=db('admin')->where('username="admin"')->field('createtime')->order('id asc')->limit(1)->find();
        $runningDays=floor((time()-$admin['createtime'])/(3600*24));

        //最后更新文章
        $lastUpdatedArticle=db('article')->order('updatetime desc')->limit(1)->field('updatetime')->find();
        if(!empty($lastUpdatedArticle)){
            $lastUpdated=date('Y-m-d H:i:s',$lastUpdatedArticle['updatetime']);
        }else{
            $lastUpdated=date('Y-m-d H:i:s');
        }

        //统计数据
        $statisticalData=array(
            array('文章总数',db('article')->count().'篇'),
            array('评论留言',(db('article_comment')->count()+db('message')->count()).'条'),
            array('分类目录',db('article_category')->count().'个'),
            array('文章标签',db('tags')->count().'个'),
            array('友情链接',db('friendship')->count().'个'),
            /*array('网站运行',$runningDays.'天'),*/
            array('浏览总量',db('article')->sum('clicks').'次'),
            array('最后更新',$lastUpdated),
        );

        $this->assign('pageInfo',$pageInfo);
        $this->assign('parentNavigation',$parentNavigation);
        $this->assign('articleList',$articleList);
        $this->assign('show',$show);
        $this->assign('bannerList',$bannerList);
        $this->assign('childrenPlist',$childrenPlist);
        $this->assign('hotArticleList',$hotArticleList);
        $this->assign('categoryList',$categoryList);
        $this->assign('statisticalData',$statisticalData);
        $this->assign('id',$id);
        $this->assign('identify_1',$identify);
        return $this->view->fetch();
    }

}